<template>
    <div style="display: flex;flex-direction: row;align-items: center;">
        <button @click="simulateEsc()" class="button">
                <div class="button-box">
                    <span class="button-elem">
                        <svg viewBox="0 0 46 40" xmlns="http://www.w3.org/2000/svg">
                            <path
                                d="M46 20.038c0-.7-.3-1.5-.8-2.1l-16-17c-1.1-1-3.2-1.4-4.4-.3-1.2 1.1-1.2 3.3 0 4.4l11.3 11.9H3c-1.7 0-3 1.3-3 3s1.3 3 3 3h33.1l-11.3 11.9c-1 1-1.2 3.3 0 4.4 1.2 1.1 3.3.8 4.4-.3l16-17c.5-.5.8-1.1.8-1.9z">
                            </path>
                        </svg>
                    </span>
                    <span class="button-elem">
                        <svg viewBox="0 0 46 40">
                            <path
                                d="M46 20.038c0-.7-.3-1.5-.8-2.1l-16-17c-1.1-1-3.2-1.4-4.4-.3-1.2 1.1-1.2 3.3 0 4.4l11.3 11.9H3c-1.7 0-3 1.3-3 3s1.3 3 3 3h33.1l-11.3 11.9c-1 1-1.2 3.3 0 4.4 1.2 1.1 3.3.8 4.4-.3l16-17c.5-.5.8-1.1.8-1.9z">
                            </path>
                        </svg>
                    </span>
                </div>
            </button>
            <span style="font-size: 20px;">相关作品</span>
    </div>
    <div v-if="userStore.userDetailsList.length > 0" class="article-list">
        <div v-for="article in userStore.userDetailsList" :key="article.id" class="article-card">
            <div style="cursor: pointer;" @click="openAricleDetails(article.id)">
                <div class="card-header">
                    <h3 class="card-title">{{ article.title }}</h3>
                </div>
                <div class="card-body">
                    <p class="card-content">{{ truncateContent(article.content, 100) }}</p>
                    <img v-if="article.pic" :src="`${config.downloadUrl}?name=${article.pic}`" alt="作品图片"
                        class="card-image" />
                </div>
                <div class="card-footer">
                    <p class="card-meta">浏览量: {{ article.viewCount }}</p>
                    <p class="card-meta">发布时间: {{ formatDate(article.createTime) }}</p>
                </div>
            </div>
        </div>
    </div>
    <div v-else class="empty-tip">
        <p>暂无相关作品</p>
    </div>
</template>

<script setup>
import { useUserStore } from "../stores/user";
import config from "../../public/config";
import { ElMessage } from "element-plus";
import { selectUserPublish } from "../api/article";
import { onMounted } from "vue";
import router from "../router";
import { useForYouStore } from "../stores/foryou";

const userStore = useUserStore();
const foryouStore = useForYouStore()
onMounted(() => {
    getFollowerArticles()
})

// 截断内容
function truncateContent(content, maxLength) {
    if (content.length > maxLength) {
        return content.slice(0, maxLength) + '...';
    }
    return content;
}
function simulateEsc() {
    router.back()
}
// 格式化日期
function formatDate(dateString) {
    const date = new Date(dateString);
    return date.toLocaleDateString(); // 你可以根据需要格式化日期
}
async function getFollowerArticles() {
    const params = new URLSearchParams
    params.append("userId", userStore.userDetailsId)
    await selectUserPublish(params).then(res => {
        if (res.code === 200) {
            userStore.userDetailsList = res.data.list
        }
    })
}
function openAricleDetails(id) {
    foryouStore.isdrawerOpen = true;
  foryouStore.articleId = id;
  router.push('/articleDetails')
}
</script>

<style scoped>
.user-header {
    display: flex;
    align-items: center;
}

.username {
    margin-left: 10px;
    font-weight: bold;
}

.deleteFollow {
    margin-left: 10px;
    font-weight: bold;
    color: blueviolet;
    cursor: pointer;
    transition: text-decoration 0.3s ease;
}

.deleteFollow:hover {
    text-decoration: underline;
}

.article-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
    padding: 20px;
}

.article-card {
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
}

.article-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}

.card-header {
    padding: 16px;
    border-bottom: 1px solid #eee;
}

.card-title {
    margin: 0;
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

.card-body {
    padding: 16px;
}

.card-content {
    margin: 0;
    font-size: 14px;
    color: #666;
    line-height: 1.5;
}

.card-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
    margin-top: 12px;
    border-radius: 4px;
}

.card-footer {
    padding: 16px;
    background-color: #f9f9f9;
    border-top: 1px solid #eee;
}

.card-meta {
    margin: 0;
    font-size: 12px;
    color: #999;
}

.empty-tip {
    text-align: center;
    padding: 20px;
    font-size: 14px;
    color: #999;
}

.button {
    display: block;
    position: relative;
    width: 56px;
    height: 56px;
    margin: 0;
    overflow: hidden;
    outline: none;
    background-color: transparent;
    cursor: pointer;
    border: 0;
}

.button:before,
.button:after {
    content: "";
    position: absolute;
    border-radius: 50%;
    inset: 7px;
}

.button:before {
    border: 4px solid #0d2ad2;
    transition: opacity 0.4s cubic-bezier(0.77, 0, 0.175, 1) 80ms,
        transform 0.5s cubic-bezier(0.455, 0.03, 0.515, 0.955) 80ms;
}

.button:after {
    border: 4px solid #040013;
    transform: scale(1.3);
    transition: opacity 0.4s cubic-bezier(0.165, 0.84, 0.44, 1),
        transform 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    opacity: 0;
}

.button:hover:before,
.button:focus:before {
    opacity: 0;
    transform: scale(0.7);
    transition: opacity 0.4s cubic-bezier(0.165, 0.84, 0.44, 1),
        transform 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.button:hover:after,
.button:focus:after {
    opacity: 1;
    transform: scale(1);
    transition: opacity 0.4s cubic-bezier(0.77, 0, 0.175, 1) 80ms,
        transform 0.5s cubic-bezier(0.455, 0.03, 0.515, 0.955) 80ms;
}

.button-box {
    display: flex;
    position: absolute;
    top: 0;
    left: 0;
}

.button-elem {
    display: block;
    width: 20px;
    height: 20px;
    margin: 17px 18px 0 18px;
    transform: rotate(180deg);
    fill: #190a65;
}

.button:hover .button-box,
.button:focus .button-box {
    transition: 0.4s;
    transform: translateX(-56px);
}
</style>